var utils = require("../../../utils/util.js");
var app = getApp();
Page({
  data: {
    arr: [],
    pageIndex:1,
    pageSize:6,
    hasData:true
  },
  onLoad:function(){
    this.setData({
      dominameImg: utils.dominameImg,
      windowHeight: app.globalData.windowHeight
    });
    this.ajax();
  },
  stopProgration:function(e){

  },
  ajax:function(){
    var that = this;
    //wx.showLoading();
    wx.request({
      url: utils.dominame + '/api/v1/goods/goods_collect_list',
      data: {
        token: app.globalData.token,
        pageSize: that.data.pageSize,
        pageIndex: that.data.pageIndex
      },
      method: "GET",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      complete: function (res) {
        //wx.hideLoading();
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          var arrBox;
          if (that.data.pageIndex == 1) {
            arrBox = res.data.data.rows;
          } else {
            arrBox = that.data.arr.concat(res.data.data.rows)
          };
          if (that.data.pageSize * that.data.pageIndex < res.data.data.recordCount) {
            var num = that.data.pageIndex + 1;
            that.setData({
              arr: arrBox,
              hasData: true,
              pageIndex: num
            });
          } else {
            that.setData({
              arr: arrBox,
              hasData: false
            });
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          })
        }
      }
    })
  },
  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    this.setData({
      pageIndex: 1,
      hasData: true
    });
    this.ajax();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.hasData){
      this.ajax();
    }
  },
  goDetail:function(e){
    wx.navigateTo({
      url: '../../shoppingMall/shopMallProductDetail/productDetail?id='+e.currentTarget.id,
    })
  },
  cancel:function(e){
    let that=this;
    wx.showModal({
      title: '提示',
      content: '是否删除该商品',
      success:function(result){
        if(result.confirm){
          wx.showLoading();
          wx.request({
            url: utils.dominame + '/api/v1/goods/deleteGoods_collect',
            data: {
              token: app.globalData.token,
              goods_id: e.currentTarget.id
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"  //post
            },
            complete: function (res) {
              wx.hideLoading();
            },
            success: function (res) {
              console.log(res);
              if (res.data.code == 0) {
                let index = that.data.arr.findIndex(i => i.shop_goods_id == e.currentTarget.id);
                let arrBox = that.data.arr;
                arrBox.splice(index, 1);
                that.setData({
                  arr: arrBox
                });
              }
              wx.showToast({
                title: res.data.msg,
                icon: "none"
              })
            }
          })
        }
      }
    })
  }
})